{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.examples.tutorials.mnist import input_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting MNIST_data/train-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data/train-labels-idx1-ubyte.gz\n",
      "Extracting MNIST_data/t10k-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data/t10k-labels-idx1-ubyte.gz\n",
      "Iter0, Testing Accuracy: 0.9222, Training Accuracy: 0.9190364\n",
      "Iter1, Testing Accuracy: 0.9333, Training Accuracy: 0.9312364\n",
      "Iter2, Testing Accuracy: 0.9383, Training Accuracy: 0.9385091\n",
      "Iter3, Testing Accuracy: 0.9418, Training Accuracy: 0.9451636\n",
      "Iter4, Testing Accuracy: 0.9498, Training Accuracy: 0.9494182\n",
      "Iter5, Testing Accuracy: 0.9517, Training Accuracy: 0.9529818\n",
      "Iter6, Testing Accuracy: 0.9538, Training Accuracy: 0.95705456\n",
      "Iter7, Testing Accuracy: 0.9533, Training Accuracy: 0.95707273\n",
      "Iter8, Testing Accuracy: 0.9562, Training Accuracy: 0.96107274\n",
      "Iter9, Testing Accuracy: 0.9578, Training Accuracy: 0.9614364\n",
      "Iter10, Testing Accuracy: 0.9604, Training Accuracy: 0.96321815\n",
      "Iter11, Testing Accuracy: 0.9627, Training Accuracy: 0.96623635\n",
      "Iter12, Testing Accuracy: 0.9643, Training Accuracy: 0.96787274\n",
      "Iter13, Testing Accuracy: 0.9632, Training Accuracy: 0.96861815\n",
      "Iter14, Testing Accuracy: 0.9654, Training Accuracy: 0.96909094\n",
      "Iter15, Testing Accuracy: 0.9657, Training Accuracy: 0.97043633\n",
      "Iter16, Testing Accuracy: 0.9667, Training Accuracy: 0.9716182\n",
      "Iter17, Testing Accuracy: 0.9663, Training Accuracy: 0.9728364\n",
      "Iter18, Testing Accuracy: 0.9677, Training Accuracy: 0.9734182\n",
      "Iter19, Testing Accuracy: 0.9685, Training Accuracy: 0.97441816\n",
      "Iter20, Testing Accuracy: 0.9673, Training Accuracy: 0.97456366\n",
      "Iter21, Testing Accuracy: 0.9707, Training Accuracy: 0.97587276\n",
      "Iter22, Testing Accuracy: 0.9698, Training Accuracy: 0.97654545\n",
      "Iter23, Testing Accuracy: 0.9705, Training Accuracy: 0.9763273\n",
      "Iter24, Testing Accuracy: 0.971, Training Accuracy: 0.97725457\n",
      "Iter25, Testing Accuracy: 0.9718, Training Accuracy: 0.97858185\n",
      "Iter26, Testing Accuracy: 0.9708, Training Accuracy: 0.9777273\n",
      "Iter27, Testing Accuracy: 0.9727, Training Accuracy: 0.9795455\n",
      "Iter28, Testing Accuracy: 0.971, Training Accuracy: 0.9785636\n",
      "Iter29, Testing Accuracy: 0.9725, Training Accuracy: 0.9807091\n",
      "Iter30, Testing Accuracy: 0.9726, Training Accuracy: 0.9793818\n"
     ]
    }
   ],
   "source": [
    "#load dataset\n",
    "mnist = input_data.read_data_sets(\"MNIST_data\",one_hot=True)\n",
    "\n",
    "#define batch size\n",
    "batch_size = 100\n",
    "#calculate number of batches\n",
    "n_batch = mnist.train.num_examples // batch_size\n",
    "\n",
    "#define placeholders\n",
    "x = tf.placeholder(tf.float32, [None,784])\n",
    "y = tf.placeholder(tf.float32, [None,10])\n",
    "keep_prob=tf.placeholder(tf.float32)\n",
    "\n",
    "#create simple NeuroNet\n",
    "W1 = tf.Variable(tf.truncated_normal([784,2000],stddev=0.1))\n",
    "b1 = tf.Variable(tf.zeros([2000])+0.1)\n",
    "L1 = tf.nn.tanh(tf.matmul(x,W1)+b1)\n",
    "L1_drop = tf.nn.dropout(L1,keep_prob)\n",
    "\n",
    "W2 = tf.Variable(tf.truncated_normal([2000,2000],stddev=0.1))\n",
    "b2 = tf.Variable(tf.zeros([2000])+0.1)\n",
    "L2 = tf.nn.tanh(tf.matmul(L1_drop,W2)+b2)\n",
    "L2_drop = tf.nn.dropout(L2,keep_prob)\n",
    "\n",
    "W3 = tf.Variable(tf.truncated_normal([2000,1000],stddev=0.1))\n",
    "b3 = tf.Variable(tf.zeros([1000])+0.1)\n",
    "L3 = tf.nn.tanh(tf.matmul(L2_drop,W3)+b3)\n",
    "L3_drop = tf.nn.dropout(L3,keep_prob)\n",
    "\n",
    "W4 = tf.Variable(tf.truncated_normal([1000,10],stddev=0.1))\n",
    "b4 = tf.Variable(tf.zeros([10])+0.1)\n",
    "prediction = tf.nn.softmax(tf.matmul(L3_drop,W4)+b4)\n",
    "\n",
    "#cost function\n",
    "# loss = tf.reduce_mean(tf.square(y-prediction))\n",
    "loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))\n",
    "#train with gradient descent\n",
    "train_step = tf.train.GradientDescentOptimizer(0.5).minimize(loss)\n",
    "\n",
    "#initialize variables\n",
    "init = tf.global_variables_initializer()\n",
    "\n",
    "#find accuracy of trained model\n",
    "correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))   #convert a list of booleans into a single boolean value\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    for epoch in range(31):\n",
    "        for batch in range(n_batch):\n",
    "            batch_xs,batch_ys = mnist.train.next_batch(batch_size)\n",
    "            sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys,keep_prob:0.7})\n",
    "        \n",
    "        test_acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob:1.0})\n",
    "        train_acc = sess.run(accuracy,feed_dict={x:mnist.train.images,y:mnist.train.labels,keep_prob:1.0})\n",
    "\n",
    "        print(\"Iter\" + str(epoch) + \", Testing Accuracy: \" + str(test_acc) + \", Training Accuracy: \" +str(train_acc))\n",
    "        \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "keep_prob @ 1.0:\n",
    "Iter0, Testing Accuracy: 0.9494, Training Accuracy: 0.9568\n",
    "Iter1, Testing Accuracy: 0.9592, Training Accuracy: 0.9712727\n",
    "Iter2, Testing Accuracy: 0.9644, Training Accuracy: 0.9809273\n",
    "Iter3, Testing Accuracy: 0.968, Training Accuracy: 0.9858182\n",
    "Iter4, Testing Accuracy: 0.971, Training Accuracy: 0.9884727\n",
    "Iter5, Testing Accuracy: 0.9728, Training Accuracy: 0.9913091\n",
    "Iter6, Testing Accuracy: 0.9755, Training Accuracy: 0.99216366\n",
    "Iter7, Testing Accuracy: 0.9772, Training Accuracy: 0.99307275\n",
    "Iter8, Testing Accuracy: 0.976, Training Accuracy: 0.9935455\n",
    "Iter9, Testing Accuracy: 0.9763, Training Accuracy: 0.9939455\n",
    "Iter10, Testing Accuracy: 0.9768, Training Accuracy: 0.9942545"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
